package com.yeren.weixin.gzh.weixin.operation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.yeren.weixin.gzh.weixin.config.WeixinConfig;
import com.yeren.weixin.gzh.weixin.message.resp.Article;
import com.yeren.weixin.gzh.weixin.message.resp.NewsMessage;
import com.yeren.weixin.gzh.weixin.message.resp.TextMessage;


//import com.yeren.utils.MessageExecute;

/**
 * 自动回复,自动处理工具类
 * 
 * @author liubiao
 *
 */
public class AutoReplayExecute {
	/**
	 * 处理微信发来的请求
	 * @param request
	 * @return
	 */
	public static String processRequest(HttpServletRequest request){
		//XML格式的消息数据
		String respXml=null;
		//默认返回的文本消息内容
		String respContent ="未知的消息类型！";
		try {
			//调用parseXml方法解析请求消息
			Map<String,String> requestMap=MessageExecute.parseXml(request);
			//发送方账号
			String fromUserName=requestMap.get("FromUserName");
			//开发者微信号
			String toUserName=requestMap.get("ToUserName");
			//消息创建时间
			String createTime=requestMap.get("CreateTime");
			//消息类型
			String msgType=requestMap.get("MsgType");
			//消息ID
			String msgId=requestMap.get("MsgId");
			
			System.out.println("goto AutoReplay===========>>>>>>>>>>>");
			System.out.println("fromUserName:"+fromUserName);
			System.out.println("toUserName:"+toUserName);
			System.out.println("createTime:"+createTime);
			System.out.println("msgType:"+msgType);
			System.out.println("msgId:"+msgId);
			
			
			//回复文本消息
			TextMessage textMessage=new TextMessage();
			textMessage.setToUserName(fromUserName);
			textMessage.setFromUserName(toUserName);
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(MessageExecute.RESP_MESSAGE_TYPE_TEXT);
			
			//文本消息
			if(msgType.equals(MessageExecute.REQ_MESSAGE_TYPE_TEXT)){
				//消息内容
				String content=requestMap.get("Content");
				respContent="TEXT您发送的是文本消息-->"+content;
				System.out.println("--TEXTQQQQQQQ--");
				//设置文本消息的内容
				textMessage.setContent(respContent);
				System.out.println("respContent-->:"+respContent);
				//将文本消息对象转换成XML
				respXml=MessageExecute.messageToXml(textMessage);
				System.out.println("respXml-->:"+respXml);
			}
			
			//图片消息
			else if(msgType.equals(MessageExecute.REQ_MESSAGE_TYPE_IMAGE)){
				//图片链接
				String PicUrl=requestMap.get("PicUrl");
				respContent="IMAGE您发送的是图片消息-->"+PicUrl;
				System.out.println("--IMAGEQQQQQQQQ--");
				//设置文本消息的内容
				textMessage.setContent(respContent);
				System.out.println("respContent-->:"+respContent);
				//将文本消息对象转换成XML
				respXml=MessageExecute.messageToXml(textMessage);
				System.out.println("respXml-->:"+respXml);
			}
			
			//语音消息
			else if(msgType.equals(MessageExecute.REQ_MESSAGE_TYPE_VOICE)){
				//媒体文件标识
				String MediaId=requestMap.get("MediaId");
				//文件格式
				String format=requestMap.get("Format");
				respContent="VOICE您发送的是语音消息-->"+"MediaId:"+MediaId+" format:"+format;
				System.out.println("--VOICEQQQQQQQQ--");
				//设置文本消息的内容
				textMessage.setContent(respContent);
				System.out.println("respContent-->:"+respContent);
				//将文本消息对象转换成XML
				respXml=MessageExecute.messageToXml(textMessage);
				System.out.println("respXml-->:"+respXml);
			}
			//视频消息
			else if(msgType.equals(MessageExecute.REQ_MESSAGE_TYPE_VIDEO)){
				//媒体文件标识
				String mediaId=requestMap.get("MediaId");
				//视频消息缩略图的媒体ID
				String thumMediaId=requestMap.get("ThumMediaId");
				respContent="VIDEO您发送的是视频消息-->"+"mediaId:"+mediaId+"thumMediaId:"+thumMediaId;
				System.out.println("--VIDEOQQQQQQQ--");
				//设置文本消息的内容
				textMessage.setContent(respContent);
				System.out.println("respContent-->:"+respContent);
				//将文本消息对象转换成XML
				respXml=MessageExecute.messageToXml(textMessage);
				System.out.println("respXml-->:"+respXml);
			}
			
			//地理位置消息
			else if(msgType.equals(MessageExecute.REQ_MESSAGE_TYPE_LOCATION)){
				//地理位置纬度
				String lat=requestMap.get("Location_x");
				//地理位置经度
				String lng=requestMap.get("Location_y");
				//地理位置信息
				String label=requestMap.get("Label");
				//地图缩放大小
				String scale=requestMap.get("Scale");
				respContent="LOCATION您发送的是地理位置消息"+"lat:"+lat+" lng:"+lng+" label:"+label+" scale:"+scale;
				System.out.println("--LOCATIONQQQQQQQ--");
				//设置文本消息的内容
				textMessage.setContent(respContent);
				System.out.println("respContent-->:"+respContent);
				//将文本消息对象转换成XML
				respXml=MessageExecute.messageToXml(textMessage);
				System.out.println("respXml-->:"+respXml);
			}
			
			//链接消息
			else if (msgType.equals(MessageExecute.REQ_MESSAGE_TYPE_LINK)) {
				//消息标题
				//String title=requestMap.get("Title");
				//消息描述
				//String description =requestMap.get("Description");
				//消息链接
				//String url=requestMap.get("Url");
				respContent="LINK您发送的是链接消息this is a link";
				System.out.println("--TYPE_LINK--");
				//设置文本消息的内容
				textMessage.setContent(respContent);
				System.out.println("respContent-->:"+respContent);
				//将文本消息对象转换成XML
				respXml=MessageExecute.messageToXml(textMessage);
				System.out.println("respXml-->:"+respXml);
			}
			
			//事件推送
			else if (msgType.equals(MessageExecute.REQ_MESSAGE_TYPE_EVENT)) {
				//事件类型
				String eventType=requestMap.get("Event");
				//关注
				if(eventType.equals(MessageExecute.EVENT_TYPE_SUBSCRIBE)){
					respContent="EVENT感谢您的关注";
					
					System.out.println("--SUBSCRIBE--");
				}
				//取消关注
				else if (eventType.equals(MessageExecute.EVENT_TYPE_UNSUBSCRIBE)) {
					//什么也不做，取消订阅后，用户不会再收到公众号发送的消息，因此不需要回复
					System.out.println("--UNSUBSCRIBE--");
				}
				//扫描带参数二维码
				else if (eventType.equals(MessageExecute.EVENT_TYPE_SCAN)) {
					respContent="EVENT感谢您的扫描";
					System.out.println("--SCAN--");
				}
				//上报地理位置
				else if (eventType.equals(MessageExecute.EVENT_TYPE_LOCATION)) {
					respContent="EVENT感谢您上报地理位置";
					System.out.println("--LOCATION--");
				}
				//自定义菜单
				else if (eventType.equals(MessageExecute.EVENT_TYPE_CLICK)) {
					//事件key值，与创建菜单时的key值对应
					String eventKey=requestMap.get("EventKey");
					//根据key值判断用户点击的按钮
					if(eventKey.equals("baidu")){
						Article article=new Article();
						article.setTitle("百度");
						article.setDescription("百度是个好网站……");
						article.setPicUrl("https://www.baidu.com/img/bd_logo1.png");
						article.setUrl("http://www.baidu.com");
						List<Article> articleList=new ArrayList<Article>();
						articleList.add(article);
						//创建图文消息
						NewsMessage newsMessage=new NewsMessage();
						newsMessage.setToUserName(fromUserName);
						newsMessage.setFromUserName(toUserName);
						newsMessage.setCreateTime(new Date().getTime());
						newsMessage.setMsgType(MessageExecute.RESP_MESSAGE_TYPE_NEWS);
						newsMessage.setArticleCount(articleList.size());
						newsMessage.setArticles(articleList);
						respXml=MessageExecute.messageToXml(newsMessage);
					}else if (eventKey.equals("smygzh")) {
						textMessage.setContent("微信搜索-->市民云服务<--公众号，很好用的哦！");
						respXml=MessageExecute.messageToXml(textMessage);
					}else if (eventKey.equals("LTZ")) {
						textMessage.setContent("she is a beautiful girl ...");
						respXml=MessageExecute.messageToXml(textMessage);
					}else if (eventKey.equals("oauth2")) {
						WeixinConfig wc=new WeixinConfig();
						// 第三方用户唯一凭证
						String appId = wc.getWeixin_appId();
						//回调地址
						String weixin_redirect_uri = wc.getWeixin_oauth_redirect_uri();
						String url_utf_8 = CommonExecute.urlEncodeUTF8(weixin_redirect_uri);
						String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+
								"&redirect_uri="+url_utf_8+"&response_type=code&scope=snsapi_userinfo";
						Article article=new Article();
						article.setTitle("请点击授权，您将可以享受到更多优质服务");
						article.setDescription("请猛戳这里哦^ε^");
						article.setPicUrl("http://pic33.nipic.com/20130909/13554508_091539630000_2.jpg");
						article.setUrl(url);
						List<Article> articleList=new ArrayList<Article>();
						articleList.add(article);
						
						
						//创建图文消息
						NewsMessage newsMessage=new NewsMessage();
						newsMessage.setToUserName(fromUserName);
						newsMessage.setFromUserName(toUserName);
						newsMessage.setCreateTime(new Date().getTime());
						newsMessage.setMsgType(MessageExecute.RESP_MESSAGE_TYPE_NEWS);
						newsMessage.setArticleCount(articleList.size());
						newsMessage.setArticles(articleList);
						respXml=MessageExecute.messageToXml(newsMessage);
						
					}else if (eventKey.equals("js")) {
						WeixinConfig wc=new WeixinConfig();
						String weixin_weixinJs_redirect_uri = wc.getWeixin_weixinJs_redirect_uri();
						Article article=new Article();
						article.setTitle("请点击");
						article.setDescription("请猛戳");
						article.setUrl(weixin_weixinJs_redirect_uri);
						List<Article> articleList=new ArrayList<Article>();
						articleList.add(article);
						
						
						//创建图文消息
						NewsMessage newsMessage=new NewsMessage();
						newsMessage.setToUserName(fromUserName);
						newsMessage.setFromUserName(toUserName);
						newsMessage.setCreateTime(new Date().getTime());
						newsMessage.setMsgType(MessageExecute.RESP_MESSAGE_TYPE_NEWS);
						newsMessage.setArticleCount(articleList.size());
						newsMessage.setArticles(articleList);
						respXml=MessageExecute.messageToXml(newsMessage);
					}
					System.out.println("--zidingyi menu CLICK--");
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return respXml;
		
	}
}
